<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCanel">
    <el-form>
      <el-form-item>
        <el-checkbox-group v-model="roleIds">
          <!-- 要显示角色名称 -->
          <!-- label显示要存储的值 -->
          <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="btnOK">确定</el-button>
        <el-button @click="btnCanel">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDatailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: true
    },
    // 当前要处理的用户id
    userId: {
      type: String,
      default: null // 要求必须传入
    }
  },
  data() {
    return {
      checkList: [], // 存储所有的角色
      roleIds: [] // 存储yoghurt所拥有的角色id
    }
  },
  created() {
    //   获取所有角色
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 20 }) // 默认10条数据
      this.checkList = rows
    },
    // prop
    async getUserDatailById(id) {
      const { roleIds } = await getUserDatailById(id) // 将用户所拥有的角色赋值给当前用户对象
      this.roleIds = roleIds
    },
    async btnOK() {
      await assignRoles({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.$message.success('分配角色成功')
      this.$emit('update:showRoleDialog', false)
    },
    btnCanel() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
